package cn.iocoder.yudao.module.erp.controller.admin.warningrules;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.warningrules.vo.WarningRulesPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.warningrules.vo.WarningRulesRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.warningrules.vo.WarningRulesSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.warningrules.WarningRulesDO;
import cn.iocoder.yudao.module.erp.service.warningrules.WarningRulesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 预警规则")
@RestController
@RequestMapping("/erp/warning-rules")
@Validated
public class WarningRulesController {

    @Resource
    private WarningRulesService warningRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 预警规则")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:create')")
    public CommonResult<Long> createWarningRules(@Valid @RequestBody WarningRulesSaveReqVO createReqVO) {
        return success(warningRulesService.createWarningRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 预警规则")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:update')")
    public CommonResult<Boolean> updateWarningRules(@Valid @RequestBody WarningRulesSaveReqVO updateReqVO) {
        warningRulesService.updateWarningRules(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 预警规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:delete')")
    public CommonResult<Boolean> deleteWarningRules(@RequestParam("id") Long id) {
        warningRulesService.deleteWarningRules(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 预警规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:query')")
    public CommonResult<WarningRulesRespVO> getWarningRules(@RequestParam("id") Long id) {
        WarningRulesDO warningRules = warningRulesService.getWarningRules(id);
        return success(BeanUtils.toBean(warningRules, WarningRulesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 预警规则分页")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:query')")
    public CommonResult<PageResult<WarningRulesRespVO>> getWarningRulesPage(@Valid WarningRulesPageReqVO pageReqVO) {
        PageResult<WarningRulesDO> pageResult = warningRulesService.getWarningRulesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarningRulesRespVO.class));
    }

    @GetMapping("/pageByDeptId")
    @Operation(summary = "按部门 获得ERP 预警规则分页")
    @Parameter(name = "deptId", description = "部门编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:query')")
    public CommonResult<PageResult<WarningRulesRespVO>> getWarningRulesPageByDept(@RequestParam("deptId") Long deptId) {
        WarningRulesPageReqVO pageReqVO = new WarningRulesPageReqVO();
        pageReqVO.setDeptId(String.valueOf(deptId));
        PageResult<WarningRulesDO> pageResult = warningRulesService.getWarningRulesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarningRulesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 预警规则 Excel")
    @PreAuthorize("@ss.hasPermission('erp:warning-rules:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarningRulesExcel(@Valid WarningRulesPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarningRulesDO> list = warningRulesService.getWarningRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 预警规则.xls", "数据", WarningRulesRespVO.class,
                BeanUtils.toBean(list, WarningRulesRespVO.class));
    }

}